package it.polimi.traveldream.managers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ConverterFactory {

	/**
	 * Crea una lista NON MODIFICABILE che contiene la conversione generata da converter della lista in ingresso
	 * @param entityList
	 * @param converter
	 * @return
	 */
	public static <Source, Target> List<Target> convertList(List<Source> entityList, ConverterCallback<Source, Target> converter) {
		return convertList(entityList, false, converter);
	}

	public static <Source, Target> List<Target> convertList(List<Source> entityList, boolean modifiable, ConverterCallback<Source, Target> converter) {
		List<Target> dtoList = new ArrayList<Target>(entityList.size());
		for(Source entity : entityList) {
			dtoList.add(converter.autoConvert(entity));
		}
		if(modifiable) {
			return dtoList;
		} else {
			return Collections.unmodifiableList(dtoList);
		}
	}

	public static interface ConverterCallback<Source, Target> {
		public Target autoConvert(Source dto);
	}
}
